package weaver.interfaces.solead.action;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.weaver.general.BaseBean;
import com.weaver.general.Util;
import weaver.common.DateUtil;
import weaver.conn.RecordSet;
import weaver.hrm.resource.ResourceComInfo;
import weaver.interfaces.solead.utils.SoleadUtil;
import weaver.interfaces.solead.wcf.WcfUtil;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

import java.text.SimpleDateFormat;
import java.util.Map;

/**
 * @Title: ecology-idea
 * @Company: 泛微软件
 * @author: weaver.zijs
 * @version: 1.0
 * @CreateDate: 2022-08-05
 * @Description: SM001-商贸销售订单下达
 * @ModifyLog:
 **/

public class WeaSMXSAction extends BaseBean implements Action {

    @Override
    public String execute(RequestInfo requestInfo) {
        String requestid = requestInfo.getRequestid();
        String tablename = requestInfo.getRequestManager().getBillTableName();
        RecordSet rs = new RecordSet();
        writeLog( ">>>>WeaSMXSAction" + requestid );
        JSONObject header = new JSONObject();
        JSONArray bodyList = new JSONArray();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        try {
            rs.executeQuery( "select * from " + tablename + " where requestid=?", requestid );
            rs.next();
            String mainid = Util.null2String(rs.getString("id"));
            String sqr = Util.null2String(rs.getString("sqr"));
            String khdm = Util.null2String(rs.getString("khdm"));//客户代码
            String jhrq = Util.null2String( rs.getString( "jhrq" ) );
            String bz = Util.null2String( rs.getString( "bz" ) );
            String hl = Util.null2String( rs.getString( "hl" ) );
            String jgtk = selectName(Util.null2String(rs.getString("jgtk")));
            String qyg = Util.null2String( rs.getString( "qyg" ) );
            String mdg = Util.null2String( rs.getString( "mdg" ) );
            String ftgxsl = Util.null2String( rs.getString( "ftgxsl" ) );
            String ftgxslg = Util.null2String( rs.getString( "ftgxslg" ) );
            String zghyfjsbz = Util.null2String( rs.getString( "zghyfjsbz" ) );
            String zghybxfjsbz = Util.null2String( rs.getString( "zghybxfjsbz" ) );
            String gng = Util.null2String( rs.getString( "gng" ) );
            String hdfrmb = Util.null2String( rs.getString( "hdfrmb" ) );
            String tcfrmb = Util.null2String( rs.getString( "tcfrmb" ) );
            String ywybh = Util.null2String( rs.getString( "ywybh" ) );
            String bzsm = Util.null2String( rs.getString( "bzsm" ) );
            String gdy = Util.null2String( rs.getString( "gdy" ) );
            String curdate = DateUtil.getCurrentDate();
            ResourceComInfo comInfo = new ResourceComInfo();
            String lastname = comInfo.getLastname( sqr );
            header.put( "CardCode", khdm);
            header.put( "NumAtCard", "" );
            header.put( "DocDate", WcfUtil.getFormatDate( curdate ) );
            header.put( "DocDueDate", WcfUtil.getFormatDate( jhrq ) );
            header.put( "SalesPersonCode",ywybh );
            header.put( "DocCurrency",bz );
            header.put( "DocRate", hl );
            header.put( "U_PriceTerm", jgtk );
            header.put( "U_ShipPort", qyg );
            header.put( "U_DestPort",mdg);
            header.put( "U_CNT20FT_QTY", ftgxsl );
            header.put( "U_CNT40FT_QTY", ftgxslg );
            header.put( "U_USDFee1", zghyfjsbz);
            header.put( "U_USDFee2", zghybxfjsbz );
            header.put( "U_HUB_IN", gng);
            header.put( "U_RMBFee1",hdfrmb );
            header.put( "U_RMBFee2",tcfrmb );
            header.put( "U_mercher", gdy );
            header.put( "Comments", bzsm );
            header.put( "U_FWuser",lastname );
            header.put( "U_FWProcess", requestid);
            writeLog( ">>>>>header=" + header.toJSONString() );
            rs.executeQuery( "select * from " + tablename + "_dt1  where  mainid='"+mainid+"'");
            while (rs.next()) {
                String wldm = Util.null2String( rs.getString( "wldm" ) );//物料代码
                String sl = Util.null2String( rs.getString( "sl" ) );//数量
                String phwb = Util.null2String( rs.getString( "phwb" ) );//批号文本
                String dj = Util.null2String( rs.getString( "dj" ) );//单价
                String kz = Util.null2String( rs.getString( "kz" ) );//颗重
                String gysdm = Util.null2String( rs.getString( "gysdm" ) );//供应商编码
                String cgmj = Util.null2String( rs.getString( "cgmj" ) );//采购毛价
                String bzxs = Util.null2String( rs.getString( "bzxs" ) );//包装形式  浏览按钮 问一下
                String jnzggg = Util.null2String( rs.getString( "jnzggg" ) );//	加捻纸管规格
                String xsyq = Util.null2String( rs.getString( "xsyq" ) );//	箱数要求
                String cpyt = Util.null2String( rs.getString( "cpyt" ) );//	产品用途
                String slsl = Util.null2String( rs.getString( "slsl" ) );//	 税码
                String yj = Util.null2String( rs.getString( "yj" ) );//	 佣金/KG
				String hgh = Util.null2String( rs.getString( "hgh" ) );//	货柜号 0818 陈娟
                String djpljfstsxq = Util.null2String( rs.getString( "djpljfstsxq" ) );//	 佣金/KG
                JSONObject body = new JSONObject();
                body.put( "ItemCode", wldm );
                body.put( "Quantity", sl );
                body.put( "U_BCHCode", phwb );
                body.put( "PriceAfterVAT", dj );
                body.put( "U_UntWght", kz );
                body.put( "U_Scardcode", gysdm );
                body.put( "U_PDNgprice", cgmj );
                body.put( "U_newpackfmt", bzxs );
                body.put( "U_tubesize", jnzggg );
                body.put( "U_PackReq", xsyq );
                body.put( "U_APPLICTIONS", cpyt );
                body.put( "VatGroup", slsl );
                body.put( "U_Fee4Rate", yj );
				body.put( "U_CntQTY", hgh );
                body.put( "U_linecomments", djpljfstsxq );
                writeLog( ">>>>>body=" + body.toJSONString() );
                bodyList.add( body );
            }
            writeLog( ">>>>>bodyList=" + bodyList.size() );
            String param = SoleadUtil.SMXS_XMLContent( header, bodyList );
            Map<String, String> resultMap = WcfUtil.send( param, "soleadtradetest", "17","" );
            writeLog("请求参数"+param);
            writeLog("返回结果 +"+resultMap);
            String code = resultMap.get( "code" );
            String msg = resultMap.get( "msg" );
            if (!"0".equals( code )) {
                requestInfo.getRequestManager().setMessageid( "111111" );
                requestInfo.getRequestManager().setMessagecontent( "Action接口异常," + msg );
                return Action.FAILURE_AND_CONTINUE;
            } else {
                rs.executeUpdate( "update " + tablename + " set sapdjh=?,jkbs=? where requestid=?", msg,code, requestid );
                writeLog("反写数据的sql："+"update " + tablename + " set sapdjh="+msg+",jkbs="+code+" where requestid="+requestid );
//                rs.executeUpdate( "update " + tablename + "_dt1 set sapddh=? where mainid=?", msg, mainid );
                return Action.SUCCESS;
            }
        } catch (Exception e) {
            e.printStackTrace();
            requestInfo.getRequestManager().setMessageid( "111111" );
            requestInfo.getRequestManager().setMessagecontent( "Action接口异常！" );
            return Action.FAILURE_AND_CONTINUE;
        }
    }
    public static String selectName(String id){
        RecordSet rs = new RecordSet();
        rs.execute("select  selectname from workflow_selectitem where fieldid=9093 and selectvalue =" + id);
        rs.next();
        return rs.getString("selectname");
    }

}
